package com.longdaji.decoration.ui.main.home;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.data.bean.GoodsCategory;
import com.longdaji.decoration.ui.goodslist.GoodsListActivity;
import com.longdaji.decoration.ui.goodslist.select.GoodsSelectAdapter;
import com.longdaji.decoration.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longtao on 2018/1/31.
 */

public class HomeBrandAdapter extends RecyclerView.Adapter<HomeBrandAdapter.ViewHolder> {
    private List<GoodsCategory.SecondCategory> mBrandList;
    private Context mContext;

    public HomeBrandAdapter(Context context, List<GoodsCategory.SecondCategory> brandList) {
        if (brandList == null)
            brandList = new ArrayList<>();
        this.mBrandList = brandList;
        this.mContext = context;
    }

    void setData(List<GoodsCategory.SecondCategory> list) {
        if (list == null)
            list = new ArrayList<>();
        this.mBrandList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return GoodsSelectAdapter.ITEM_TYPE.ITEM1.ordinal();
        } else {
            return GoodsSelectAdapter.ITEM_TYPE.ITEM2.ordinal();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == GoodsSelectAdapter.ITEM_TYPE.ITEM1.ordinal()) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_brand_left, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_brand_right, parent, false);
        }

        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final GoodsCategory.SecondCategory brand = mBrandList.get(position);
//        if (TextUtils.equals("发饰", brand.getCategoryName()) || TextUtils.equals("更多", brand.getCategoryName())) {
//            holder.cateName.setVisibility(View.GONE);
//            holder.expectCate.setVisibility(View.VISIBLE);
//            if (brand != null && brand.getCategoryName() != null) {
//                holder.brandName.setText(brand.getCategoryName());
//            }
//        } else {
//            holder.expectCate.setVisibility(View.GONE);
//            holder.cateName.setVisibility(View.VISIBLE);
//            if (brand != null && brand.getCategoryName() != null) {
//                holder.cateName.setText(brand.getCategoryName());
//            }
//        }
        holder.brandName.setText(brand.getCategoryName());
        ImageUtil.loadImage(mContext, brand.getImgAddress(), holder.brandPic);
        // GlideUtils.loadImage(mContext, brand.getImgAddress(), holder.brandPic, null, R.drawable.no_banner);
        // if (!TextUtils.equals("发饰", brand.getCategoryName()) && !TextUtils.equals("更多", brand.getCategoryName())) {
        holder.goodsClasses.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = GoodsListActivity.getStartIntent(mContext);
                intent.putExtra("cate_id", brand.getCateId());
                intent.putExtra("category_name", brand.getCategoryName());
                mContext.startActivity(intent);
//                Intent intent = GoodsDetailActivity.getStartIntent(mContext);
//                intent.putExtra("goods_id", "1");
//                mContext.startActivity(intent);
            }
        });
        //}
        // holder.brandName.setText(brand.getName());
        //  holder.brandPic.setBackgroundResource(brand.getDrawable());
    }

    @Override
    public int getItemCount() {
        return mBrandList == null ? 0 : mBrandList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        // LinearLayout expectCate;
        ImageView brandPic;
        TextView brandName;
        //TextView cateName;
        RelativeLayout goodsClasses;

        public ViewHolder(View itemView) {
            super(itemView);
            // cateName = (TextView) itemView.findViewById(R.id.tv_cate_name);
            brandName = (TextView) itemView.findViewById(R.id.tv_brand_name);
            brandPic = (ImageView) itemView.findViewById(R.id.iv_brand_pic);
            goodsClasses = (RelativeLayout) itemView.findViewById(R.id.flyt_goods_classes);
            // expectCate = (LinearLayout) itemView.findViewById(R.id.llyt_expect_cate);
        }
    }

}
